﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using ZHDJ.Core;
using ZHDJ.Data.Models;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal
{
    public partial class PartyOpenMore : PortalPage
    {
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "15";
            }
        }
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        private string Module
        {
            get { return SysEnum.ArticleModule.ThreeAffairOpen.ToString(); }
        }
        private string Category
        {
            get { return this.Request["Category"] ?? ""; }
        }

        private string BranchID
        {
            get
            {
                return this.Request["BranchID"] ?? "D658070EC3BF425BA8417745FDCBFCB0";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
                return;
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();
                return;
            }
            if (!IsPostBack)
            {
                LoadData();
            }

        }
        public void LoadData()
        {

            hdBranchID.Value = BranchID;

        }


        /// <summary>
        /// 截取标题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string CutLongTitle(object obj, int length)
        {
            string title = Convert.ToString(obj);
            if (title.Length > length)
            {
                title = title.Substring(0, length) + "...";
            }
            return title;
        }


        private void WriteList()
        {
            int startIndex = (Convert.ToInt32(this.PageIndex)) * Convert.ToInt32(this.PageSize);
            DataTable dt = PartyArticle.GetMoreArticleTitle(Convert.ToInt32(PageSize),
                startIndex, BranchID, SysEnum.ArticleModule.ThreeAffairOpen.ToString(),
               Category, false).Tables[0];
            dt.Columns.Add("CreateTime2", typeof(string));
            dt.Columns.Add("Title2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                dr["CreateTime2"] = Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd");
                dr["Title2"] = CutLongTitle(Convert.ToString(dr["Title"]),45);
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyArticle.GetMoreArticleTitle(10,
                0, BranchID, SysEnum.ArticleModule.ThreeAffairOpen.ToString(),
               Category, false).Tables[1].Rows[0][0]);
            Response.End();
        }

    }
}